package com.laicunba.ext.web.controller.h5.activity;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.laicunba.constant.GloabConstant;
import com.laicunba.ext.pojo.MiSportsVO;
import com.laicunba.ext.service.MiSportsService;
import com.laicunba.ext.web.controller.BaseController;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.service.RegistryService;
import com.laicunba.util.StringUtil;

@Controller
@RequestMapping(value = "/h5/ext/activity")
public class DownloadApp0717Controller extends BaseController {
	private static final Logger LOG = Logger.getLogger(DownloadApp0717Controller.class);
	@Autowired
	private RegistryService registryService;
	@Autowired
	private MiSportsService miSportsService;

	@RequestMapping(value = "/download_app_0717_form.htm")
	public ModelAndView downloadApp0717Form(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("download_app_0717.jsp");
		
		return view;
	}

	@SuppressWarnings({ "unchecked", "finally" })
	@ResponseBody
	@RequestMapping(value = "/mi_signature.htm")
	public String miSignature(HttpServletRequest request) {
		JSONObject json = new JSONObject();
		try {
			String uri = request.getRequestURI();
			uri = uri.substring(request.getContextPath().length());
			String link = GloabConstant.WEB_PATH + uri + "?" + request.getQueryString();
			
			MiSportsVO miSportsVO = new MiSportsVO();
			miSportsVO.setUrl(link);
			LOG.info(String.format("【邀请好友】url=[%s]", "u." + miSportsVO.getUrl()));
			miSportsVO = miSportsService.getMiSportsVO(miSportsVO);
			LOG.info(String.format("【邀请好友】url=[%s]", "u." + miSportsVO.getUrl()));
			json.put("status", "success");
			json.put("signature", miSportsVO.getSignature());
		} catch (Exception e) {
			json.put("status", "error");
			json.put("signature", "获取签名错误");
			e.printStackTrace();
		} finally {
			return json.toJSONString();
		}
		
	}
	
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/download_app_0717.htm")
	public String downloadApp0717(HttpServletRequest request, HttpServletResponse response, String mobile) {
		JSONObject json = new JSONObject();
		if (!StringUtil.isMobileNumber(mobile)) {
			json.put("status", 0);
		}
		if (registryService.get(RegistryDO.class, "act.0717." + mobile) == null && registryService.create("act.0717." + mobile, mobile)) {
			json.put("status", 1);
		} else {
			json.put("status", 2);
		}

		return json.toJSONString();

	}

}
